package cn.yh.nft.turbo.user.facade;

import cn.yh.nft.turbo.api.user.request.*;
import cn.yh.nft.turbo.api.user.request.condition.UserIdQueryCondition;
import cn.yh.nft.turbo.api.user.request.condition.UserPhoneAndPasswordQueryCondition;
import cn.yh.nft.turbo.api.user.request.condition.UserPhoneQueryCondition;
import cn.yh.nft.turbo.api.user.request.condition.UserPhoneQueryCondition;
import cn.yh.nft.turbo.api.user.response.UserOperatorResponse;
import cn.yh.nft.turbo.api.user.response.UserQueryResponse;
import cn.yh.nft.turbo.api.user.response.data.UserInfo;
import cn.yh.nft.turbo.api.user.service.UserFacadeService;
import cn.yh.nft.turbo.base.response.PageResponse;
import cn.yh.nft.turbo.rpc.facade.Facade;
import cn.yh.nft.turbo.user.domain.entity.User;
import cn.yh.nft.turbo.user.domain.entity.convertor.UserConvertor;
import cn.yh.nft.turbo.user.domain.service.impl.UserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @ClassName: UserFacadeServiceImpl
 * @Author: 草莓爱上啵啵
 */
@DubboService(version = "1.0.0")
public class UserFacadeServiceImpl implements UserFacadeService {

    @Autowired
    private UserService userService;

    /**
     * 根据用户查询请求查询用户信息
     * 此方法使用switch表达式来替代原有的if-else或策略模式，简化了代码结构，提高了可读性
     *
     * @param userQueryRequest 用户查询请求对象，包含用户查询条件
     * @return 返回一个UserQueryResponse对象，包含查询结果
     */
    @Override
    @Facade
    public UserQueryResponse<UserInfo> query(UserQueryRequest userQueryRequest) {

        // 使用switch表达式精简代码，根据不同的查询条件执行相应的查询逻辑
        User user = switch (userQueryRequest.getUserQueryCondition()){
            case UserIdQueryCondition userIdQueryCondition:
                // 根据用户ID查询用户信息
                yield userService.findById(userIdQueryCondition.getUserId());
            case UserPhoneQueryCondition userPhoneQueryCondition:
                // 根据用户电话号码查询用户信息
                yield userService.findByTelephone(userPhoneQueryCondition.getTelephone());
            case UserPhoneAndPasswordQueryCondition userPhoneAndPasswordQueryCondition:
                // 根据用户电话号码和密码查询用户信息
                yield userService.findByTelephoneAndPass(userPhoneAndPasswordQueryCondition.getTelephone(), userPhoneAndPasswordQueryCondition.getPassword());
            default:
                // 如果查询条件不支持，则抛出异常
                throw new UnsupportedOperationException(userQueryRequest.getUserQueryCondition() + "'' is not supported");
        };

        // 创建用户查询响应对象并设置成功标志为true
        UserQueryResponse<UserInfo> userQueryResponse = new UserQueryResponse();
        userQueryResponse.setSuccess(true);

        // 将查询到的用户信息转换为UserInfo对象
        UserInfo userInfo = UserConvertor.INSTANCE.mapToVo(user);
        // 将转换后的用户信息设置到响应对象中
        userQueryResponse.setData(userInfo);

        // 返回用户查询响应对象
        return userQueryResponse;
    }


    @Override
    @Facade
    public PageResponse<UserInfo> pageQuery(UserPageQueryRequest userPageQueryRequest) {
        var queryResult = userService.pageQueryByState(userPageQueryRequest.getKeyword(), userPageQueryRequest.getState(),userPageQueryRequest.getCurrentPage(), userPageQueryRequest.getPageSize());
        PageResponse<UserInfo> response =new PageResponse<>();
        if (!queryResult.getSuccess()) {
            response.setSuccess(false);
            return response;
        }

        response.setSuccess(true);
        response.setDatas(UserConvertor.INSTANCE.mapToVo(queryResult.getDatas()));
        response.setCurrentPage(queryResult.getCurrentPage());
        response.setPageSize(queryResult.getPageSize());
        return response;
    }

    @Override
    @Facade
    public UserOperatorResponse register(UserRegisterRequest userRegisterRequest) {
        return userService.register(userRegisterRequest.getTelephone(), userRegisterRequest.getInviteCode());
    }

    @Override
    @Facade
    public UserOperatorResponse modify(UserModifyRequest userModifyRequest) {
        return null;
    }

    @Override
    @Facade
    public UserOperatorResponse auth(UserAuthRequest userAuthRequest) {
        return userService.auth(userAuthRequest);
    }

    @Override
    @Facade
    public UserOperatorResponse active(UserActiveRequest userActiveRequest) {
        return userService.active(userActiveRequest);
    }
}
